<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column 
        prop="userid" 
        label="用户ID" 
        width="100" 
        align="center"
        :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="150"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column prop="phone" label="电话" width="120"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="role" label="角色" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.role === 'admin' ? 'primary' : 'success'">
            {{ scope.row.role === 'admin' ? '管理员' : '普通用户' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button 
              @click="edit(scope.row)" 
              type="primary" 
              icon="el-icon-edit" 
              size="small"
              circle
              style="margin-right: 10px">
            </el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button 
              @click="deleteUser(scope.row)" 
              type="danger" 
              icon="el-icon-delete" 
              size="small"
              circle>
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination 
      background 
      layout="prev, pager, next, sizes, jumper, ->, total"
      :page-sizes="[5, 10, 20]"
      :page-size="pageSize" 
      :total="total" 
      :current-page="currentPage"
      @current-change="page"
      @size-change="handleSizeChange"
      style="margin-top: 20px;">
      <template #default="{pager}">
        <span class="el-pagination__total">
          共 {{ total }} 条记录 第{{ currentPage }}/{{ Math.ceil(total/pageSize) }}页
        </span>
      </template>
    </el-pagination>
  </div>
</template>

<script>
export default {
  methods: {
    deleteUser(row) {
      const _this = this
      axios.delete('http://localhost:8181/user/deleteById/' + row.userid).then(function (resp) {
        _this.$alert('用户 ' + row.username + ' 删除成功！', '消息', {
          confirmButtonText: '确定',
          callback: action => {
            _this.fetchData(_this.currentPage)
          }
        })
      })
    },
    edit(row) {
      this.$router.push({
        path: '/admin/user/update',
        query: {
          userid: row.userid
        }
      })
    },
    page(currentPage) {
      this.currentPage = currentPage
      this.fetchData(currentPage)
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.currentPage = 1
      this.fetchData(1)
    },
    fetchData(page) {
      const _this = this
      // 计算总页数
      const totalPages = Math.ceil(_this.total / _this.pageSize)
      
      // 如果请求页码超出范围，重置为第一页
      if (page > totalPages && totalPages > 0) {
        page = 1
        _this.currentPage = 1
      }
      
      const pageIndex = page - 1 // 转换为0-based索引
      console.log('请求分页数据 - 前端页码:', page, '后端页码参数:', pageIndex, '每页大小:', _this.pageSize, '完整URL:', `http://localhost:8181/user/findAll/${pageIndex}/${_this.pageSize}`)
      
      // 显示加载状态
      _this.tableData = []
      _this.loading = true
      
        axios.get(`http://localhost:8181/user/findAll/${pageIndex}/${_this.pageSize}`)
        .then(function (resp) {
      console.log('分页响应数据:', {
        请求页码: page,
        返回数据量: resp.data.data.length,
        数据ID列表: resp.data.data.map(user => user.userid),
        每页大小: _this.pageSize,
        总记录数: resp.data.total
      })
      
      if (!resp.data || !resp.data.data) {
        throw new Error('响应数据格式错误')
      }
      
      _this.tableData = resp.data.data
      _this.total = resp.data.total || 0
      _this.currentPage = page
        
      }).catch(error => {
        console.group('请求错误详情')
        console.error('错误信息:', error.message)
        console.error('错误堆栈:', error.stack)
        if (error.response) {
          console.error('响应状态:', error.response.status)
          console.error('响应数据:', error.response.data)
        }
        console.groupEnd()
        
        _this.$message.error(`加载数据失败: ${error.message}`)
      }).finally(() => {
        _this.loading = false
      })
    }
  },

  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: []
    }
  },

  created() {
    this.fetchData(1)
  }
}
</script>
